/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mobiwebinc.compconn.factory;
import com.mobiwebinc.compconn.announcer.Announcer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author suraj
 */
public class AnnouncerFactory {
    private static AnnouncerFactory announcerFactory;
    private static Announcer announcer;
    public static AnnouncerFactory getInstance() {
        if(announcerFactory == null)
            announcerFactory = new AnnouncerFactory();
        return announcerFactory ;
    }

    public Announcer getAnnouncer() {
        try {
            if (announcer == null) {
                if (isBonjourPresent()) {
                    announcer = ((Announcer) Class.forName("com.mobiwebinc.compconn.announcer.BonjourAnnouncer").newInstance());
                } else {
                    announcer = ((Announcer) Class.forName("com.mobiwebinc.compconn.announcer.DummyAnnouncer").newInstance());
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AnnouncerFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AnnouncerFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AnnouncerFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return announcer;
    }

    public boolean isBonjourPresent() {
        try {
            Class.forName("com.apple.dnssd.DNSSDService");
        } catch (ClassNotFoundException ex) {
            System.err.println("Bonjour not present " + ex.getMessage());
            return false;
        }
        return true ;
    }
    
}
